package com.ruoyi.mobile.project.oms.service;

import com.github.pagehelper.PageHelper;
import com.ruoyi.db.oms.domain.OmsAftersale;
import com.ruoyi.db.oms.domain.query.OmsAftersaleQueryVo;
import com.ruoyi.db.oms.mapper.OmsAftersaleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;

@Service
public class WxOmsAftersaleService
{
    @Autowired
    private OmsAftersaleMapper aftersaleMapper;

    public OmsAftersale findById(Long id)
    {
        return aftersaleMapper.selectOmsAftersaleById(id);
    }

    public OmsAftersale findById(Long userId, Long id)
    {
        OmsAftersaleQueryVo aftersaleQueryVo = new OmsAftersaleQueryVo();
        aftersaleQueryVo.setId(id);
        aftersaleQueryVo.setUserId(userId);

        return aftersaleMapper.selectOmsAftersaleofUserById(aftersaleQueryVo);
    }

    /**
     * 查询售后列表
     *
     * @author 严方亚
     * @date 2020-05-15
     */
    public List<OmsAftersale> queryList(Long userId, Integer status, Integer page, Integer limit, String sort, String order)
    {
        // 赋值
        OmsAftersaleQueryVo aftersaleQueryVo = new OmsAftersaleQueryVo();
        aftersaleQueryVo.setUserId(userId);
        if (status != null) {
            aftersaleQueryVo.setStatus(status);
        }
        // 排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            aftersaleQueryVo.setOrder(order);
            aftersaleQueryVo.setSort(sort);
        }
        else{
            aftersaleQueryVo.setOrder("desc");
            aftersaleQueryVo.setSort("add_time");
        }

        // 分页
        PageHelper.startPage(page, limit);
        return aftersaleMapper.selectOmsAftersaleList(aftersaleQueryVo);
    }

    public List<OmsAftersale> querySelective(Long orderId, String aftersaleSn, Short status, Integer page, Integer limit, String sort, String order)
    {
        return null;
    }

    private String getRandomNum(Integer num)
    {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++)
        {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public int countByAftersaleSn(Long userId, String aftersaleSn)
    {
        return 0;
    }

    // TODO 这里应该产生一个唯一的编号，但是实际上这里仍然存在两个售后编号相同的可能性
    public String generateAftersaleSn(Long userId)
    {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        String now = df.format(LocalDate.now());
        String aftersaleSn = now + getRandomNum(6);
        while (countByAftersaleSn(userId, aftersaleSn) != 0)
        {
            aftersaleSn = now + getRandomNum(6);
        }
        return aftersaleSn;
    }

    /**
     * 添加售后
     *
     * @author 严方亚
     * @date 2020-05-14
     * @param aftersale
     */
    public void add(OmsAftersale aftersale)
    {
        aftersale.setAddTime(LocalDateTime.now());
        aftersale.setUpdateTime(LocalDateTime.now());
        aftersaleMapper.insertOmsAftersale(aftersale);
    }

    public void deleteByIds(List<Integer> ids)
    {

    }

    public void deleteById(Long id)
    {

    }

    /**
     * 删除售后，逻辑删除
     *
     * @author 严方亚
     * @date 2020-05-13
     * @param userId
     * @param orderId
     */
    public void deleteByOrderId(Long userId, Long orderId)
    {
        aftersaleMapper.deleteOmsAftersaleByUserIdAOId(userId, orderId);
    }

    public void updateById(OmsAftersale aftersale)
    {

    }

    /**
     * 依据orderId和userId查询售后
     *
     * @author 严方亚
     * @date 2020-05-14
     * @param userId
     * @param orderId
     */
    public OmsAftersale findByOrderId(Long userId, Long orderId)
    {
        OmsAftersaleQueryVo aftersaleQueryVo = new OmsAftersaleQueryVo();
        aftersaleQueryVo.setOrderId(orderId);
        aftersaleQueryVo.setUserId(userId);
        return aftersaleMapper.selectOmsAftersaleofUserByIdAOId(aftersaleQueryVo);
    }
}
